# Writer

// 定义 since JDK 1.1
public abstract class Writer
extends Object
implements Appendable, Closeable, Flushable

// 基本操作
public void write​(char[] cbuf) throws IOException  // 输出全部字符数组
public abstract void write​(char[] cbuf, int off, int len) throws IOException //部分
public void write​(String str) throws IOException // 输出字符串(重要)
// 输出单个字符,可以使用append
1
2
3
4
5
6
7
8
9
10
Appendable
// JDK 1.5
public interface Appendable {
    Appendable append​(char c) throws IOException;
    Appendable append​(CharSequence csq) throws IOException;
    Appendable append​(CharSequence csq, int start, int end) throws IOException;
}
1
2
3
4
5
6

代码样例

点击查看
// 实现类 继承关系:FileWriter -> OutputStreamWriter -> Writer
public FileWriter(File file) throws IOException
public FileWriter(File file, boolean append) throws IOException

public static void main(String[] args) {
    // 1. 定义路径
    File file = new File("d:" + File.separator + "demo.txt"); 
    if (!file.getParentFile().exists()) {
        file.getParentFile().mkdirs(); //创建父路径
    }
    // 2. 选择子类实现抽象类 
    Writer out = new FileWriter(file);
    // 3. 数据输出
    out.write("hello");
    out.append​("hhh");  // 追加
    // 4. 关闭流
    out.close();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

# Reader

// 定义 since JDK1.1
public abstract class Reader
extends Object
implements Readable, Closeable

// 基本操作
public int read() throws IOException // 读取一个字符
public int read​(char[] cbuf) throws IOException // 读取内容到字符数组
public abstract int read​(char[] cbuf, int off, int len) throws IOException
1
2
3
4
5
6
7
8
9
Readable
// JDK1.5
// CharBuffer 属于 java.nio 包下,JDK1.4后提出
public interface Readable {
    int read​(CharBuffer cb) throws IOException;
}
1
2
3
4
5

代码样例

点击查看
// 实现类 继承关系:FileReader -> InputStreamReader -> Reader
public FileReader(File file) throws FileNotFoundException

public static void main(String[] args) {
    // 1. 定义路径
    File file = new File("d:" + File.separator + "demo.txt"); 
    if (file.exists()) {
        // 2. 选择子类实现抽象类 
        Reader in = new FileReader(file);
        char[] data = new char[1024];
         // 3. 实现数据的输入,将数据读入字符数组
        int len = in.read(data); // 数组可能未装满,需要部分转换为字符串
        System.out.println(new String(data, 0, len));  
        // 4. 关闭流
        in.close();

        // try-with-resource
        try (Reader in = new FileReader(file)) {
            char[] data = new char[1024];
            int len = in.read(data);
            System.out.println(new String(data, 0, len));  
        } catch (Exception e) {
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Last Updated: 7/1/2020, 2:19:02 AM